package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	fileName := "test.txt"
	dir, _ := os.Getwd()
	dirPath := filepath.Join(dir, fileName)
	file, err := os.Open(dirPath)
	if err != nil {
		// 文件可能不存在，先创建一个文件
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		return
	}
	_, err = file.WriteString(" ")  // 文件是只读的，尝试写入会返回错误
	var underlyingErr string
	if _, ok := err.(*os.PathError); ok {
		underlyingErr = "(path error)"
	}
	fmt.Fprintf(os.Stderr, "ERROR: %v %s\n", err, underlyingErr)
}
